package com.siruier.boss.ui.activity.mall

import android.view.View
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.recyclerview.widget.GridLayoutManager
import com.google.android.material.appbar.AppBarLayout
import com.kingja.loadsir.core.LoadService
import com.siruier.boss.api.MALL_API_SERVICE
import com.siruier.boss.api.core.launchUI
import com.siruier.boss.api.core.toData
import com.siruier.boss.api.loginApi
import com.siruier.boss.bean.MallGoodsBean
import com.siruier.boss.common.ACTION_MAIN_CHANGE_TAB
import com.siruier.boss.common.GOODS_SPAN_COUNT
import com.siruier.boss.databinding.ActivityZeroMoneyWelfareBinding
import com.siruier.boss.ui.adapter.BossBannerImageAdapter
import com.siruier.boss.ui.adapter.GoodsAdapter
import com.siruier.boss.ui.api.ApiObserver
import com.siruier.boss.ui.base.BaseActivity
import com.siruier.boss.ui.ext.showLoadingCallback
import com.siruier.boss.ui.helper.InjectBundle
import com.siruier.boss.ui.helper.RefreshHelper
import com.siruier.boss.ui.helper.loadsir.LoadSirHelper
import com.siruier.boss.ui.helper.localSendBroadcast
import com.siruier.boss.ui.listener.OnTabSelectListener
import com.youth.banner.indicator.CircleIndicator

class ZeroMoneyWelfareActivity : BaseActivity<ActivityZeroMoneyWelfareBinding>() {
    private val currentTab: Int by InjectBundle()
    private lateinit var rootLoadService: LoadService<*>
    private lateinit var mRefreshHelper: RefreshHelper<MallGoodsBean>
    private lateinit var mGoodsAdapter: GoodsAdapter
    private var mLayoutManager: GridLayoutManager? = null
    override fun initView() {
        bindViewClick(vb.ivWelfareGo, vb.ivToUp)
        rootLoadService = LoadSirHelper.register(vb.refreshLayout, listener = ::loadData)
        rootLoadService.showLoadingCallback()
        vb.tabSegment.setTabData(arrayOf("0元福利", "每日宠你"))
        vb.tabSegment.currentTab = currentTab
        vb.tabSegment.setOnTabSelectListener(OnTabSelectListener { mRefreshHelper.onRefresh() })
        mGoodsAdapter = GoodsAdapter()
        mRefreshHelper =
            RefreshHelper(vb.refreshLayout, vb.rvGoods, mGoodsAdapter, mLoadSirView = vb.rvGoods, listener = ::goodsList)
        mLayoutManager = GridLayoutManager(this, GOODS_SPAN_COUNT)
        vb.rvGoods.layoutManager = mLayoutManager

        vb.banner.setAdapter(BossBannerImageAdapter())
        vb.banner.indicator = CircleIndicator(mThis)
        vb.banner.addBannerLifecycleObserver(this)
        loadData()
    }

    override fun onClick(v: View?) {
        when (v) {
            vb.ivWelfareGo -> {
                localSendBroadcast(ACTION_MAIN_CHANGE_TAB) {
                    putInt("position", 1)
                }
                finish()
            }
            vb.ivToUp -> {
                val behavior = (vb.appBarLayout.layoutParams as? CoordinatorLayout.LayoutParams)?.behavior
                if (behavior is AppBarLayout.Behavior) {
                    val topAndBottomOffset = behavior.topAndBottomOffset
                    if (topAndBottomOffset != 0) {
                        behavior.topAndBottomOffset = 0
                    }
                    mLayoutManager?.scrollToPositionWithOffset(0, 0)
                }
            }
        }
    }

    private fun loadData() {
        launchUI(
            {
                vb.banner.setDatas(loginApi.banner(3).toData())
                loadGoods()
            }, ApiObserver(
                showMsg = false,
                loadService = rootLoadService,
                apiObserver = mRefreshHelper.handApiObserver()
            )
        )
    }

    private fun goodsList() {
        launchUI({ loadGoods() }, mRefreshHelper.handApiObserver())
    }

    private suspend fun loadGoods(): MutableList<MallGoodsBean>? {
        if (vb.tabSegment.currentTab == 0) {
            return MALL_API_SERVICE.zeroPage(null, mRefreshHelper.page).toData()?.records
        } else if (vb.tabSegment.currentTab == 1) {
            return MALL_API_SERVICE.homePage(null, mRefreshHelper.page).toData()?.records
        }
        return null
    }
}